

<?php

class User extends Controller {

    function User() {
        parent::Controller();
        $this->load->model(array('mod_user'));
    }

    //========================function for load page master====================//

    function index() {
        if($this->check_session()){
        $this->load->view('master');
        }else{
            redirect('admin/');
            exit();
        }
    }

    //========================this is function for loading page master===========//

    function manager() {
        if($this->check_session()){
        $data['select_data'] = $this->mod_user->selectuser();
        $this->load->view('master', $data);
        $data['title'] = 'user';
        }else{
            redirect('admin/');
            exit();
        }
    }

    //===========================this is function add new user and validate form-==========================//

    function add() {
        if($this->check_session()){
        $data['title'] = 'this is the add new page';
        $this->load->helper("mgpage");
        $this->form_validation->set_rules('txtuser', 'Username', 'required|trim|max_length[50]');
        // $this->form_validation->set_rules('txpassword', 'Password', 'required|trim|max_length[50]');
        $this->form_validation->set_rules('txtcon_word', 'Confirm Password', 'required|trim|max_length[50]');
        $this->form_validation->set_rules('txtemail', 'Email', 'required|trim|valid_email|max_length[50]');
        if ($this->input->post('btn_submit')) {// if submit form
            if ($this->form_validation->run() == false) {// if validate is false
                $this->load->view('master', $data);
            } else {//if validate form is true
                $this->mod_user->add();
                $this->session->set_userdata('ms', '<br/><p><dl id="system-message"><dd class="message message"><ul><li>Add Successful!</li></ul></dd></dl></p>');
                redirect('user/manager');
            }
        } else {// loading master page
            $this->load->view('user/add');
        }
        }  else {
            redirect('admin/');
            exit();
        }
    }

    //==========================function is used for edit data from tbl_user==============================//

    function edit() {
        if($this->check_session()){
        if ($this->input->post('btn_submit')) {// if form submit, it will edit record
            $this->mod_user->checkedit();
            $this->session->set_userdata('edit_ms', '<br/><dl id="system-message"><dd class="message message"><ul><li>Edited Successful!</li></ul></dd></dl>');
            redirect('user/manager');
        } else {//if form is not submit, it will view form adding 
            $data['edit_data'] = $this->mod_user->selet_edit();
            $this->load->view('master', $data);
        }
        }else{
            redirect('admin/');
            exit();
        }
    }

    //===========================function is used for delete data from tbl_user========================//

    function delete() {
        if($this->check_session()){
        $this->mod_user->delete();
        $this->session->set_userdata('del_ms', '<br/><dl id="system-message"><dd class="message message"><ul><li>Deleted Successful!</li></ul></dd></dl>');
        redirect('user/manager');
        }  else {
            redirect('admin/');
            exit();
        }
    }

//===========================function  is the end of  used for delete data from tbl_user========================//
    
     //========this is function for using checking session=============
    
     public function check_session() {
        if ($this->session->userdata('use_username')) {
            return true;
        } else {
            return false;
        }
    }
}